package com.yjm.tb.view;

import java.util.Random;

import com.yjm.tb.R;
import com.yjm.tb.widget.ChoseTypeDlg;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class TbSwitch extends Activity {
	private ImageView imgView;
	Random random = new Random();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tbswitch);
		initResources();
	}

	private boolean mSwitching = false;
	private final Handler mImgHandler = new Handler();
	private final Handler mTimerHandler = new Handler();
	
	public void initResources()
	{
		imgView = (ImageView)findViewById(R.id.switchImg);
		imgView.setImageBitmap(TbCamera.imgList.get(0));
		
		imgView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startSwitch();
				mTimerHandler.postDelayed(stopSwitch, 3000);
			}
		});
	}
	
	private void startSwitch() {
		if (!mSwitching) {
			mSwitching = true;
			imgView.setClickable(false);
			mImgHandler.postDelayed(updateImg, 80);
		}
	}

	private Runnable stopSwitch = new Runnable(){
		@Override
		public void run(){
			mSwitching = false;
			imgView.setClickable(true);
			
			new ChoseTypeDlg(TbSwitch.this).showDlg();
			
		}
	};
	
	protected Runnable updateImg = new Runnable(){
		@Override
		public void run() {
			if (mSwitching)
			{
				imgView.setImageBitmap(TbCamera.imgList.get(random.nextInt(TbCamera.imgList.size())));
				mImgHandler.postDelayed(updateImg, 80);
			}
		}
	};

}
